'''
Created on 19/04/2010

@author: Mar
'''

from CPU import *

from planifCortoPlazo_FF import *
from planifCortoPlazo_Priority import *


from ThreadCPU import *
from ThreadCPU import *
from CPU import *
from multiprocessing import Queue
import time
from IOPrinter import *
from ThreadPrinter import *
from IOScreen import *
from ThreadScreen import *
from IOKeyBoard import *
from ThreadKeyBoard import *
from ResourceManager import *





class OS:
        
    def __init__(self):

        self.scheduler = Priority(self)
        self.readyQueue = self.scheduler.readyQueue
        self.printQueue = Queue()
        self.kbQueue = Queue()
        self.newQueue = Queue()
        self.screenQueue = Queue()
        self.collOfResources = {"kb" : self.kbQueue, "screen" : self.screenQueue, "printer" : self.printQueue}
        self.resourceManager = ResourceManager(self.collOfResources)
        self.keyBoard = IOKeyBoard(self.readyQueue)
        self.printer = IOPrinter(self.readyQueue)
        self.screen = IOScreen(self.readyQueue)
        self.CPU = CPU(self.readyQueue, self.resourceManager)
        self.CPU2 = CPU(self.readyQueue, self.resourceManager)
        self.cpuThread = ThreadCPU(self.CPU,self.scheduler)
        self.cpuThread2 = ThreadCPU(self.CPU2,self.scheduler)
        self.printThread  = ThreadPrinter (self.printQueue, self.printer, self.readyQueue)
        self.screenThread = ThreadScreen (self.screenQueue, self.screen, self.readyQueue)
        self.keyBoardThread = ThreadKeyBoard(self.kbQueue, self.keyBoard, self.readyQueue) 
        
        self.cpuThread.setDaemon(True)
        self.cpuThread2.setDaemon(True)
        self.printThread.setDaemon(True)
        self.screenThread.setDaemon(True)
        self.keyBoardThread.setDaemon(True)
        
    def setReadyQueue(self,aQueue):
        self.readyQueue= aQueue
    
    def addProgram(self,aProgram):
        pcb = aProgram.toPCB()
        self.newQueue.put(pcb)
        x = self.newQueue.get()
        x.toReady()
        #self.readyQueue.put(x)
        self.scheduler.addPCB(x,self.readyQueue)
        
        
    def startUP(self):
        self.cpuThread.start()
        self.cpuThread2.start()
        self.printThread.start()
        self.screenThread.start()
        self.keyBoardThread.start()
        
        time.sleep(15)
        
    def shutDown(self):
        # Estos _stop() tiran un error raro, y de todas maneras no sabia que se podia
        # usar ese _stop(). podes usar este exit(), que mata a todos los threads, si
        # antes los seteaste como q son "daemon". Un thread es daemon cuando son infinitos
        # como tus 2 threads que tienen while true. Asi que para usar el exit y matarlos
        # hay que decirles a esos threads que son daemons.
        # En el __init__ hice eso asi lo ves
        print("FIN") # El fin va antes porque el exit mata todo y no imprimiria nada
        exit()
        
    
    
